package com.loft.threadtask;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Set;

import com.loft.comm.util.LogCenter;
import com.loft.model.ImageLoader;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

public class BitmapAsynctask extends AsyncTask<String, Void, Bitmap> {
	private final WeakReference<ImageView> imageViewReference;
	private File imgFile;
	private String urlString;
	private byte[] data;
	public static final String TAG = "BitmapDownloaderTask";
	private Set<String> urlSet;
	private HashMap<String, Bitmap> cache;

	public BitmapAsynctask(ImageView imageView,
			HashMap<String, Bitmap> cache, File file, Set<String> urlSet

	) {
		this.imageViewReference = new WeakReference<ImageView>(imageView);
		this.imgFile = file;
		this.urlSet = urlSet;
		this.cache = cache;
	}

	@Override
	protected Bitmap doInBackground(String... params) {
		synchronized (urlSet) {
			try {
				urlString = params[0];
				return getImage(urlString);
			} catch (Exception e) {
				urlSet.remove(urlString);// 当timeout时，从set里删除该url，以便之后继续刷新
				return null;
			}
		}
	}

	/**
	 * 
	 * FunName: getImage Description: Create Date: 2012/04/18
	 */
	public Bitmap getImage(String path) throws Exception {
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setReadTimeout(10 * 1000);
		InputStream inputStream = conn.getInputStream();
		data = readInputStream(inputStream);
		Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
		return bitmap;

	}

	@Override
	protected void onPostExecute(Bitmap bitmap) {
		if (isCancelled()) {
			bitmap = null;
		}
		if (imageViewReference != null) {
			ImageView imageView = imageViewReference.get();
			if (imageView != null) {
				if (bitmap != null && bitmap.getWidth() > 5) {
					OutputStream outputStream;
					try {
						imgFile.createNewFile();
						outputStream = new FileOutputStream(imgFile);
						outputStream.write(data);
						outputStream.flush();
						outputStream.close();
					} catch (Exception e) {
						LogCenter.getInstance().error(TAG,
								"--------------->>>" + e.toString());
					}
					cache.put(urlString, bitmap);
					BitmapFactory.Options options = new Options();
					Bitmap bitmap_thumbnail = BitmapFactory.decodeByteArray(
							data, 0, data.length, options);
					imageView.setImageBitmap(bitmap_thumbnail);
					bitmap.recycle();
				}

			}
		}
	}

	public byte[] readInputStream(InputStream inputStream) throws IOException {
		byte[] buffer = new byte[1024];
		int len = 0;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while ((len = inputStream.read(buffer)) != -1) {
			bos.write(buffer, 0, len);
		}
		bos.close();
		return bos.toByteArray();

	}

}
